import { h } from 'vue';

import dashboard from './modules/dashboard'
import deviceAlarm from './modules/device-alarm'
import smartDevice from './modules/smart-device'
import smartDeviceList from './modules/smart-device-list'
import system from './modules/system'

import BasicLayout from '@/layouts/basic-layout'

/**
 * 在主框架内显示
 */
const frameIn = [
  {
    path: '/',
    redirect: {
      name: 'dashboard-console'
    },
    component: BasicLayout,
    children: [
      {
        path: 'index',
        name: 'index',
        redirect: {
          name: 'dashboard-console'
        }
      },
      {
        path: 'log',
        name: 'system-log',
        meta: {
          title: '前端日志',
          auth: true,
          isEmpty: true
        },
        component: () => import('@/pages/system/log')
      },
      {
        path: 'user-center',
        name: 'system-user-center',
        meta: {
          title: '个人中心',
          auth: true,
          isEmpty: true
        },
        component: () => import('@/pages/setting/user')
      },
      {
        path: 'user-setting',
        name: 'system-user-setting',
        meta: {
          title: '个人设置',
          auth: true,
          isEmpty: true
        },
        component: () => import('@/pages/setting/account')
      },
      // 刷新页面 必须保留
      {
        path: 'refresh',
        name: 'refresh',
        hidden: true,
        component: {
          beforeRouteEnter (to, from, next) {
            next(instance => instance.$router.replace(from.fullPath));
          },
          render: () => h()
        }
      },
      // 页面重定向 必须保留
      {
        path: 'redirect/:route*',
        name: 'redirect',
        hidden: true,
        component: {
          beforeRouteEnter (to, from, next) {
            next(instance => instance.$router.replace(JSON.parse(from.params.route)));
          },
          render: () => h()
        }
      },
    ]
  },
  dashboard,
  deviceAlarm,
  smartDevice,
  smartDeviceList,
  system
];

/**
 * 在主框架之外显示
 */
const frameOut = [
  // 登录
  {
    path: '/login',
    name: 'login',
    meta: {
      title: '登录'
    },
    component: () => import('@/pages/account/login')
  },
  // 注册
  {
    path: '/register',
    name: 'register',
    meta: {
      title: '注册'
    },
    component: () => import('@/pages/account/register')
  },
  // 注册结果
  {
    path: '/register/result',
    name: 'register-result',
    meta: {
      auth: true,
      title: '注册结果'
    },
    component: () => import('@/pages/account/register/result')
  }
];

/**
 * 错误页面
 */
const errorPage = [
  {
    path: '/403',
    name: '403',
    meta: {
      title: '403'
    },
    component: () => import('@/pages/system/error/403')
  },
  {
    path: '/500',
    name: '500',
    meta: {
      title: '500'
    },
    component: () => import('@/pages/system/error/500')
  },
  {
    path: '/:pathMatch(.*)',
    name: '404',
    meta: {
      title: '404'
    },
    component: () => import('@/pages/system/error/404')
  }
];

// 导出需要显示菜单的
export const frameInRoutes = frameIn;

// 重新组织后导出
export default [
  ...frameIn,
  ...frameOut,
  ...errorPage
];
